<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.8/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">路径</label>
                            <div class="layui-input-inline">
                                <input type="text" name="like_with_path" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">描述</label>
                            <div class="layui-input-inline">
                                <input type="text" name="like_with_title" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">扩展</label>
                            <div class="layui-input-inline">
                                <input type="text" name="extension" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

    </div>
</div>
<textarea style="position: absolute; top: 0; left: 0; opacity: 0; z-index: -10;" id="id_textarea_for_copy"></textarea>
<script src="/lib/layui-v2.6.8/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=2.0.0" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'upload'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            upload = layui.upload,
            table = layui.table;

        $.get('/admin/user/getCurrent', {}, function (re) {
            if (re.code == 0) {
                layui.use('watermark', function (watermark) {
                    watermark({watermark_txt: re.data.name});
                });
            }
        }, 'json');

        layui.copyText = function (text) {
            let input = document.getElementById("id_textarea_for_copy");
            input.value = text; // 修改文本框的内容
            input.select(); // 选中文本
            document.execCommand("copy");
            layer.msg('已复制');
        };

        layui.delOne = function (id, path, extension) {
            layer.confirm('真的删除么？', function (index) {
                $.post('/admin/static/delete', {
                    'id': id,
                    'path': path,
                    'extension': extension
                }, function (re) {
                    if (re.code == 0) {
                        table.reload('currentTableId');
                    } else {
                        layer.msg(re.msg);
                    }
                }, 'json');
                layer.close(index);
            });
        };

        table.render({
            elem: '#currentTableId',
            url: '/admin/static/list',
            where: {
                order: 'id desc'
            },
            cols: [[
                {field: 'id', width: 60, title: 'ID', sort: true},
                {field: 'path', title: '路径', templet: function (row) {
                    if (row.path == '') {
                        return '上传新增';
                    }
                    return row.path;
                }},
                {field: 'title', title: '<i class="layui-icon layui-icon-edit"></i>描述 (单击可编辑保存)', edit: 'text', templet: function (row) {
                    return '<span id="id_span_title_' + row.id + '">' + row.title + '</span>';
                }},
                {field: 'extension', title: '类型', width: 75},
                {field: 'created_at', title: '上传时间', width: 160},
                {title: '查看', width: 85, templet: function (row) {
                    if (row.id == 0) {
                        return '';
                    }
                    return '<a class="layui-btn layui-btn-warm layui-btn-xs" target="_blank" href="' + row.url + '"><i class="layui-icon layui-icon-download-circle"></i>查看</a>';
                }},
                {title: '复制', width: 85, templet: function (row) {
                    if (row.id == 0) {
                        return '';
                    }
                    return '<button class="layui-btn layui-btn-xs" onclick="javascript:layui.copyText(\'' + row.url + '\');"><i class="layui-icon layui-icon-link"></i>复制</button>';
                }},
                {title: '上传', width: 85, templet: function (row) {
                    let title = '上传新文件' + (row.id == 0 ? '' : '，以替换原文件。');
                    let url = '/admin/static/' + (row.id == 0 ? 'create' : 'update');
                    let data = 'id:\'' + row.id + '\',path:\'' + row.path + '\',extension:\'' + row.extension + '\'';
                    data += ',title:function(){return layui.jquery(\'#id_span_title_' + row.id + '\').html();}';
                    return '<button type="button" title="' + title + '" class="layui-btn layui-btn-normal layui-btn-xs my-upload-btn" lay-data="{url: \'' + url + '\',data:{' + data + '}}"><i class="layui-icon layui-icon-upload-drag"></i>上传</button>';
                }},
                {title: '删除', width: 85, templet: function (row) {
                    if (row.id == 0) {
                        return '';
                    }
                    return '<button type="button" class="layui-btn layui-btn-danger layui-btn-xs" onclick="javascript:layui.delOne(' + row.id + ', \'' + row.path + '\', \'' + row.extension + '\');"><i class="layui-icon">&#xe640;</i>删除</button>';
                }}
            ]],
            limits: [50, 100],
            limit: 50,
            page: true,
            skin: 'line',
            done: function (res, curr, count) {
                upload.render({
                    elem: '.my-upload-btn',
                    accept: 'file',
                    before: function () {
                        layer.load(2);
                    },
                    done: function (re) {
                        $('body, html').animate({
                            scrollTop: 0
                        }, 100);
                        if (re.code == 0) {
                            table.reload('currentTableId');
                        } else {
                            layer.msg(re.msg);
                        }
                        layer.closeAll('loading');
                    }
                });
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            table.reload('currentTableId', {
                page: {curr: 1},
                where: data.field
            }, 'data');
            return false;
        });

        // 监听单元格编辑
        table.on('edit(currentTableFilter)', function(obj) {
            let field = obj.field;
            let id = obj.data.id;
            let value = obj.value.trim();
            if (field != 'title') {
                return false;
            }
            let load = layer.load(2);
            $.post('/admin/static/update', {'id': id, 'title': value}, function (re) {
                layer.close(load);
                if (re.code != 0) {
                    layer.msg(re.msg);
                }
            }, 'json');
        });

    });
</script>

</body>
</html>